<script setup>
import { ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import { useMediaQuery } from '@vueuse/core'
import zhCn from 'element-plus/es/locale/lang/zh-cn'
import {
  House,
  Tools,
  Monitor,
  Document,
  Connection,
  Link,
  Timer,
  Search,
  Brush,
  Edit,
  Moon,
  Sunny
} from '@element-plus/icons-vue'

const route = useRoute()
const activeMenu = ref(route.path)
const isDark = ref(false)

// 监听路由变化
watch(() => route.path, (newPath) => {
  activeMenu.value = newPath
})

// 切换主题
const toggleTheme = (value) => {
  const html = document.documentElement
  if (value) {
    html.classList.add('dark')
  } else {
    html.classList.remove('dark')
  }
}

// 初始化主题
isDark.value = window.matchMedia('(prefers-color-scheme: dark)').matches
toggleTheme(isDark.value)
</script>

<template>
  <el-config-provider :locale="zhCn">
    <el-container class="app-container">
      <el-header class="app-header" height="60px">
        <div class="header-left">
          <img src="./assets/vue.svg" class="logo" alt="Logo" />
          <h1 class="site-title">前端工具集</h1>
        </div>

        <el-menu
          :default-active="activeMenu"
          class="app-menu"
          mode="horizontal"
          :router="true"
        >
          <el-menu-item index="/">
            <el-icon><House /></el-icon>
            <span>首页</span>
          </el-menu-item>

          <el-sub-menu index="/tools">
            <template #title>
              <el-icon><Tools /></el-icon>
              <span>工具箱</span>
            </template>
            
            <el-menu-item index="/tools/subnet">
              <el-icon><Monitor /></el-icon>
              <span>子网掩码计算器</span>
            </el-menu-item>
            
            <el-menu-item index="/tools/json">
              <el-icon><Document /></el-icon>
              <span>JSON 格式化</span>
            </el-menu-item>
            
            <el-menu-item index="/tools/base64">
              <el-icon><Connection /></el-icon>
              <span>Base64 编解码</span>
            </el-menu-item>
            
            <el-menu-item index="/tools/url">
              <el-icon><Link /></el-icon>
              <span>URL 编解码</span>
            </el-menu-item>
            
            <el-menu-item index="/tools/timestamp">
              <el-icon><Timer /></el-icon>
              <span>时间戳转换</span>
            </el-menu-item>
            
            <el-menu-item index="/tools/regex">
              <el-icon><Search /></el-icon>
              <span>正则表达式</span>
            </el-menu-item>
            
            <el-menu-item index="/tools/color">
              <el-icon><Brush /></el-icon>
              <span>颜色选择器</span>
            </el-menu-item>
            
            <el-menu-item index="/tools/markdown">
              <el-icon><Edit /></el-icon>
              <span>Markdown 预览</span>
            </el-menu-item>
          </el-sub-menu>
        </el-menu>

        <div class="header-right">
          <el-switch
            v-model="isDark"
            class="theme-switch"
            inline-prompt
            :active-icon="Moon"
            :inactive-icon="Sunny"
            @change="toggleTheme"
          />
        </div>
      </el-header>

      <el-main class="app-main">
        <router-view v-slot="{ Component }">
          <transition name="fade" mode="out-in">
            <component :is="Component" />
          </transition>
        </router-view>
      </el-main>
    </el-container>
  </el-config-provider>
</template>

<style>
/* 全局样式 */
html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: var(--el-font-family);
}

#app {
  height: 100%;
}

/* 布局样式 */
.app-container {
  height: 100vh;
}

.app-header {
  background-color: var(--el-bg-color);
  border-bottom: 1px solid var(--el-border-color-light);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 200px;
}

.logo {
  height: 32px;
  width: 32px;
}

.site-title {
  font-size: 20px;
  font-weight: 600;
  color: var(--el-text-color-primary);
  margin: 0;
  white-space: nowrap;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 16px;
  min-width: 100px;
  justify-content: flex-end;
}

.theme-switch {
  margin-left: auto;
}

.app-menu {
  flex: 1;
  justify-content: center;
  border-bottom: none;
}

.app-menu :deep(.el-menu-item),
.app-menu :deep(.el-sub-menu__title) {
  font-size: 15px;
  height: 60px;
  line-height: 60px;
}

.app-menu :deep(.el-menu-item .el-icon),
.app-menu :deep(.el-sub-menu__title .el-icon) {
  margin-right: 4px;
}

.app-main {
  background-color: var(--el-bg-color-page);
  padding: 80px 20px 20px;
  min-height: 100vh;
  overflow-y: auto;
}

/* 过渡动画 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

/* 响应式布局 */
@media (max-width: 768px) {
  .app-header {
    padding: 0 10px;
  }

  .site-title {
    display: none;
  }

  .app-menu {
    padding: 0 5px;
  }

  .app-menu :deep(.el-menu-item),
  .app-menu :deep(.el-sub-menu__title) {
    padding: 0 10px;
    font-size: 14px;
  }

  .header-right {
    min-width: 60px;
  }
}

@media (max-width: 480px) {
  .app-main {
    padding: 70px 10px 10px;
  }

  .app-menu :deep(.el-menu-item),
  .app-menu :deep(.el-sub-menu__title) {
    padding: 0 8px;
    font-size: 13px;
  }
}
</style>
